<template>
  <div>
    <el-card class="box-card margin_30">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <el-form :model="formData" label-width="80px" :inline="true" size="mini">
        <el-form-item label="作者">
          <el-input v-model="formData.author" />
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="编辑类型">
          <el-select v-model="formData.editorType" placeholder="编辑类型">
            <el-option
              label="富文本"
              value="0"
            />
            <el-option
              label="markown"
              value="1"
            />
          </el-select>
        </el-form-item>
        <el-button size="mini" @click="reset">重置</el-button>
        <el-button size="mini" type="primary" @click="search">搜索</el-button>
      </el-form>
    </el-card>

    <el-card class="box-card margin_30">
      <div slot="header" class="clearfix">
        <el-button type="primary" @click="goOperation">新增</el-button>
      </div>
      <div>
        <el-table
          :data="artileList"
          border
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="#"
            fixed="left"
            width="50"
            align="center"
          />
          <el-table-column
            prop="title"
            label="文章标题"
            align="center"
            width="250"
          />
          <el-table-column
            prop="coverImg"
            label="展示图片"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <img style="width:100px; height:100px" :src="scope.row.coverImg" alt="">
              <!-- {{ scope.row.coverImg }} -->
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="添加时间"
            width="300"
            align="center"
          />
          <el-table-column
            prop="author"
            label="文章作者"
            align="center"
            width="200"
          />
          <el-table-column
            prop="isShow"
            label="文章是否展示"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isShow"
                active-color="#409eff"
                @change="upStatus($event,scope.row.id)"
              />
            </template>

          </el-table-column>
          <el-table-column
            label="内容"
            align="center"
            width="200"
          >
            <el-button type="primary">富文本框</el-button>
          </el-table-column>
          <el-table-column
            prop=""
            label="操作"
            fixed="right"
            align="center"
            width="150"
          >
            <template slot-scope="scope">
              <el-link icon="el-icon-view" type="primary" @click="goOperation(scope.row.id)">编辑文章</el-link>
              <el-link type="danger" @click="delArticle(scope.row.id)">删除</el-link>
            </template>

          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="start"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { findArticles, del, changeShowStatus } from '@/api/content/article/index'
import myMixins from '@/mixins/index'
export default {
  name: 'VueAdminTemplateMasterIndex',
  mixins: [myMixins],
  data() {
    return {
      artileList: [],
      formData: {
        'author': '',
        'collectCount': '',
        'content1': '',
        'content2': '',
        'coverImg': '',
        'createTime': '',
        'editorType': '',
        'id': '',
        'isShow': 0,
        'modifyTime': '',
        'summary': '',
        'title': '',
        'viewCount': 0,
        'zanCount': 0
      }
    }
  },

  created() {
    this.init()
  },

  mounted() {

  },

  methods: {
    upStatus(e, id) {
      console.log(e, id)
      var isShow = e ? 1 : 0
      changeShowStatus({
        isShow, id
      }).then(res => {
        this.$message({
          message: '修改状态成功',
          type: 'success'
        })
        this.init()
      })
    },
    delArticle(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    goOperation(id) {
      if (id) {
        this.$router.push(`/content/operation?id=${id}`)
      } else {
        this.$router.push('/content/operation')
      }
    },
    reset() {
      this.formData.author = ''
      this.formData.title = ''
      this.formData.editorType = ''
      this.init()
    },
    search() {
      this.init()
    },
    init() {
      findArticles(this.start, this.limit, this.formData).then(res => {
        console.log(res)
        res.data.rows.forEach(ele => {
          ele.isShow = !!ele.isShow
        })
        this.total = res.data.total
        this.artileList = res.data.rows
      })
    }
  }
}
</script>

<style scoped>

</style>
